# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def levelOrder(self, root):
        """
        :type root: TreeNode
        :rtype: List[List[int]]
        """
        queue = []
        if root is None:
            return queue
        res = []
        queue.append(root)
        while queue:
            size = len(queue)
            lev = []
            while size > 0:
                now = queue[0]
                lev.append(now.val)
                del queue[0]
                if now.left is not None:
                    queue.append(now.left)
                if now.right is not None:
                    queue.append(now.right)
                size -= 1
            res.append(lev)
        return res